﻿using System;
using System.Management;

namespace 获取硬件信息
{
    /// <summary>
    /// 获取硬件信息方法类
    /// </summary>
    public class GetDrive
    {
        #region 获取MAC地址方法
        /// <summary>
        /// 获取MAC地址方法
        /// </summary>
        /// <returns>返回MAC地址</returns>
        public string GetMACString()
        {
            ManagementClass mAdapter = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection mo = mAdapter.GetInstances();
            foreach (ManagementBaseObject m in mo)
            {
                if ((bool)m["IpEnabled"] == true)
                {
                    return m["MacAddress"].ToString();
                }
            }
            mo.Dispose();
            return "";// null;
        }
        #endregion

        #region 获取CPU个数
        /// <summary>
        /// 获取CPU个数
        /// </summary>
        /// <returns>返回CPU个数</returns>
        public int GetCpuCount()
        {
            try
            {
                using (ManagementClass mCpu = new ManagementClass("Win32_Processor"))
                {
                    ManagementObjectCollection cpus = mCpu.GetInstances();
                    return cpus.Count;
                }
            }
            catch
            {
            }
            return -1;
        }
        #endregion

        #region 获取CPU的频率 这里之所以使用string类型的数组，主要是因为cpu的多核
        /// <summary>
        /// 获取CPU的频率 这里之所以使用string类型的数组，主要是因为cpu的多核
        /// </summary>
        /// <returns>返回CPU频率合集</returns>        
        public string[] GetCpuMHZ()
        {
            ManagementClass mc = new ManagementClass("Win32_Processor");
            ManagementObjectCollection cpus = mc.GetInstances();
            string[] mHz = new string[cpus.Count];
            int c = 0;
            ManagementObjectSearcher mySearch = new ManagementObjectSearcher("select * from Win32_Processor");
            foreach (ManagementObject mo in mySearch.Get())
            {
                mHz[c] = mo.Properties["CurrentClockSpeed"].Value.ToString();
                c++;
            }
            mc.Dispose();
            mySearch.Dispose();
            return mHz;
        }
        #endregion

        #region 获取硬盘的大小
        /// <summary>
        /// 获取硬盘的大小
        /// </summary>
        /// <returns>硬盘大小</returns>
        public string GetSizeOfDisk()
        {
            ManagementClass mc = new ManagementClass("Win32_DiskDrive");
            ManagementObjectCollection moj = mc.GetInstances();
            foreach (ManagementObject m in moj)
            {
                return m.Properties["Size"].Value.ToString();
            }
            return "-1";
        }
        #endregion

        #region 获取内存大小
        /// <summary>
        /// 获取内存的大小
        /// </summary>
        /// <returns>返回内存大小</returns>
        public string GetSizeOfMemery()
        {
            ManagementClass mc = new ManagementClass("Win32_OperatingSystem");
            ManagementObjectCollection moc = mc.GetInstances();
            double sizeAll = 0.0;
            foreach (ManagementObject m in moc)
            {
                if (m.Properties["TotalVisibleMemorySize"].Value != null)
                {
                    sizeAll += Convert.ToDouble(m.Properties["TotalVisibleMemorySize"].Value.ToString());
                }
            }
            mc = null;
            moc.Dispose();
            return sizeAll.ToString();
        }
        #endregion

        #region 获得CPU编号
        /// <summary>
        /// 获得CPU编号
        /// </summary>
        /// <returns>返回CPU编号</returns>
        public string GetCpuid()
        {
            var cpuid = string.Empty;
            var mc = new ManagementClass("Win32_Processor");
            var moc = mc.GetInstances();
            foreach (var o in moc)
            {
                var mo = (ManagementObject)o;
                cpuid = mo.Properties["ProcessorId"].Value.ToString();
            }
            return cpuid;
        }
        #endregion

        #region 获取硬盘序列号
        /// <summary>
        /// 获取硬盘序列号
        /// </summary>
        /// <returns>返回硬盘序列号</returns>
        public string GetDiskId()
        {
            //这种模式在插入一个U盘后可能会有不同的结果，如插入我的手机时
            var hDid = string.Empty;
            var mc = new ManagementClass("Win32_DiskDrive");
            var moc = mc.GetInstances();
            foreach (var o in moc)
            {
                var mo = (ManagementObject)o;
                hDid = (string)mo.Properties["Model"].Value;
                //这名话解决有多个物理盘时产生的问题，只取第一个物理硬盘
                break;
            }
            return hDid;
        }
        #endregion

        #region 获取网卡硬件地址
        /// <summary>
        /// 获取网卡硬件地址
        /// </summary>
        /// <returns>返回网卡硬件地址</returns>
        public string GetMacAddress()
        {
            var mac = "";
            var mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            var moc = mc.GetInstances();
            foreach (var o in moc)
            {
                var mo = (ManagementObject)o;
                if (!(bool)mo["IPEnabled"]) continue;
                mac = mo["MacAddress"].ToString();
                break;
            }
            return mac;
        }
        #endregion

        #region 获取IP地址
        /// <summary>
        /// 获取IP地址
        /// </summary>
        /// <returns>返回IP地址</returns>
        public string GetIpAddress()
        {
            var st = string.Empty;
            var mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            var moc = mc.GetInstances();
            foreach (var o in moc)
            {
                var mo = (ManagementObject)o;
                if (!(bool)mo["IPEnabled"]) continue;
                var ar = (Array)(mo.Properties["IpAddress"].Value);
                st = ar.GetValue(0).ToString();
                break;
            }
            return st;
        }
        #endregion

        #region 获取操作系统的登录用户名
        /// <summary>
        /// 获取操作系统的登录用户名
        /// </summary>
        /// <returns>返回操作系统的登录用户名</returns>
        public string GetUserName()
        {
            return Environment.UserName;
        }
        #endregion

        #region 获取计算机名
        /// <summary>
        /// 获取计算机名
        /// </summary>
        /// <returns>返回计算机名</returns>
        public string GetComputerName()
        {
            return Environment.MachineName;
        }
        #endregion

        #region 获取操作系统类型
        /// <summary>
        /// 获取操作系统类型
        /// </summary>
        /// <returns>返回操作系统类型</returns>
        public string GetSystemType()
        {
            var st = string.Empty;
            var mc = new ManagementClass("Win32_ComputerSystem");
            var moc = mc.GetInstances();
            foreach (var o in moc)
            {
                var mo = (ManagementObject)o;
                st = mo["SystemType"].ToString();
            }
            return st;
        }
        #endregion

        #region 获取物理内存
        /// <summary>
        /// 获取物理内存
        /// </summary>
        /// <returns>返回物理内存</returns>
        public string GetPhysicalMemory()
        {
            var st = string.Empty;
            var mc = new ManagementClass("Win32_ComputerSystem");
            var moc = mc.GetInstances();
            foreach (var o in moc)
            {
                var mo = (ManagementObject)o;
                st = mo["TotalPhysicalMemory"].ToString();
            }
            return st;
        }
        #endregion

        #region 获取显卡PNPDeviceID
        /// <summary>
        /// 获取显卡PNPDeviceID
        /// </summary>
        /// <returns>返回显卡PNPDeviceID</returns>
        public string GetVideoPnpId()
        {
            var st = "";
            var mos = new ManagementObjectSearcher("Select * from Win32_VideoController");
            foreach (var o in mos.Get())
            {
                var mo = (ManagementObject)o;
                st = mo["PNPDeviceID"].ToString();
            }
            return st;
        }
        #endregion

        #region 获取声卡PNPDeviceID
        /// <summary>
        /// 获取声卡PNPDeviceID
        /// </summary>
        /// <returns>返回声卡PNPDeviceID</returns>
        public string GetSoundPnpId()
        {
            var st = string.Empty;
            var mos = new ManagementObjectSearcher("Select * from Win32_SoundDevice");
            foreach (var o in mos.Get())
            {
                var mo = (ManagementObject)o;
                st = mo["PNPDeviceID"].ToString();
            }
            return st;
        }
        #endregion

        #region 获取CPU版本信息
        /// <summary>
        /// 获取CPU版本信息
        /// </summary>
        /// <returns>返回CPU版本信息</returns>
        public string GetCpuVersion()
        {
            var st = string.Empty;
            var mos = new ManagementObjectSearcher("Select * from Win32_Processor");
            foreach (var o in mos.Get())
            {
                var mo = (ManagementObject)o;
                st = mo["Version"].ToString();
            }
            return st;
        }
        #endregion

        #region 获取CPU名称信息
        /// <summary>
        /// 获取CPU名称信息
        /// </summary>
        /// <returns>返回CPU名称信息</returns>
        public string GetCpuName()
        {
            var st = string.Empty;
            var driveId = new ManagementObjectSearcher("Select * from Win32_Processor");
            foreach (var o in driveId.Get())
            {
                var mo = (ManagementObject)o;
                st = mo["Name"].ToString();
            }
            return st;
        }
        #endregion

        #region 获取CPU制造厂商
        /// <summary>
        /// 获取CPU制造厂商
        /// </summary>
        /// <returns>返回CPU制造厂商</returns>
        public string GetCpuFirm()
        {
            var st = string.Empty;
            var mos = new ManagementObjectSearcher("Select * from Win32_Processor");
            foreach (var o in mos.Get())
            {
                var mo = (ManagementObject)o;
                st = mo["Manufacturer"].ToString();
            }
            return st;
        }
        #endregion

        #region 获取主板制造厂商
        /// <summary>
        /// 获取主板制造厂商
        /// </summary>
        /// <returns>返回主板制造厂商</returns>
        public string GetBoardFirm()
        {
            var query = new SelectQuery("Select * from Win32_BaseBoard");
            var mos = new ManagementObjectSearcher(query);
            var data = mos.Get().GetEnumerator();
            data.MoveNext();
            var board = data.Current;
            return board.GetPropertyValue("Manufacturer").ToString();
        }
        #endregion

        #region 获取主板编号
        /// <summary>
        /// 获取主板编号
        /// </summary>
        /// <returns>返回主板编号</returns>
        public string GetBoardId()
        {
            var st = string.Empty;
            var mos = new ManagementObjectSearcher("Select * from Win32_BaseBoard");
            foreach (var o in mos.Get())
            {
                var mo = (ManagementObject)o;
                st = mo["SerialNumber"].ToString();
            }
            return st;
        }
        #endregion

        #region 获取主板型号
        /// <summary>
        /// 获取主板型号
        /// </summary>
        /// <returns>返回主板型号</returns>
        public string GetBoardType()
        {
            var st = string.Empty;
            var mos = new ManagementObjectSearcher("Select * from Win32_BaseBoard");
            foreach (var o in mos.Get())
            {
                var mo = (ManagementObject)o;
                st = mo["Product"].ToString();
            }
            return st;
        } 
        #endregion
    }
}
